/******************************************************************************
 * @brief    软件定时器<依赖于b_list模块>
 ******************************************************************************/
#ifndef __APP_TIMER_H__
#define __APP_TIMER_H__

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>

#include "b_list.h"

#define TIMER_TICKCNT_MAX	0xFFFFFFFF

//#define TIMER_MEASURE_MAX	10

#define B_CONCAT(p1, p2)      LIST_CONCAT(p1, p2)
typedef enum
{
    B_TIMER_MODE_SINGLE_SHOT,                 
    B_TIMER_MODE_REPEATED        
} b_timer_mode_t;

typedef void (*b_timerout_handler_t)(void);

typedef struct{
	void *	list_next;
	uint32_t                    end_val;     
	uint32_t                    repeat_period; 
	b_timerout_handler_t				handler;       
	uint8_t            					active;   
}b_timer_t;

typedef b_timer_t * b_timer_id_t;

#define _B_TIMER_DEF(timer_id)                                                              \
    static b_timer_t B_CONCAT(timer_id,_data) = {                                           \
            .active = 0,                                                                \
    };                                                                                      \
    const b_timer_id_t timer_id = &B_CONCAT(timer_id,_data)
			
	
		
extern uint32_t b_get_sys_timer(void);
		
extern void timer_update(void);
	
extern uint8_t b_timer_create(b_timer_id_t const *      p_timer,
                            b_timer_mode_t            mode,
                            b_timerout_handler_t timeout_handler);

extern uint8_t b_timer_start(b_timer_id_t const *      p_timer, uint32_t timeout_ticks);

extern uint8_t b_timer_stop(b_timer_id_t const *      p_timer);		
			
extern uint8_t b_timer_reload(b_timer_id_t const * p_timer, uint32_t timeout_ticks);
#ifdef TIMER_MEASURE_MAX
extern void b_measure_start(void);

extern uint8_t b_measure_stop(uint8_t id);

extern uint32_t b_measure_get(uint8_t id);
#endif
		
#endif
